/**
 * 448. 找到所有数组中消失的数字
 * https://leetcode-cn.com/problems/find-all-numbers-disappeared-in-an-array/
 */
const { displayBaseArray } = require('../../libs/public');

/**
 * @param {any[]} arr
 * @param {number} a
 * @param {number} b
 */
function swap(arr, a, b) {
  const temp = arr[a];
  arr[a] = arr[b];
  arr[b] = temp;
}

/**
 * @param {number[]} nums
 * @return {number[]}
 */
function findDisappearedNumbers(nums) {
  let target = 0;
  for (let i = 0; i < nums.length; i += 1) {
    target = nums[i] - 1;
    while (nums[target] !== nums[i]) {
      swap(nums, i, target);
      target = nums[i] - 1;
    }
  }
  const result = [];
  for (let i = 0; i < nums.length; i += 1) {
    target = i + 1;
    if (nums[i] !== target) {
      result.push(target);
    }
  }
  return result;
}

console.log(displayBaseArray(findDisappearedNumbers([4, 3, 2, 7, 8, 2, 3, 1])) === '[5,6]');
console.log(displayBaseArray(findDisappearedNumbers([1, 1])) === '[2]');
